/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.saturn.component.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import edu.ccut.saturn.component.IBundleComponentInfo;
import edu.ccut.saturn.component.IComponentInfo;
import edu.ccut.saturn.component.ILogger;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.SaturnLoggerManager;

/**
 * {@link IBundleComponentInfo} implementation
 * 
 * @author LeslieGu
 * 
 */
public class SaturnBundleComponentInfo implements IBundleComponentInfo {

	private static final String LOGGER_KEY_LOAD_BUNDLE = SaturnLoggerManager.LOGGER_KEY_LOAD_BUNDLE;

	protected static ILogger logger = SaturnLoggerManager.getInstance()
			.getSaturnLogger(LOGGER_KEY_LOAD_BUNDLE);

	private String bundlesSymbolicName;

	private Map<String, IComponentInfo> componentInfoMap;

	public SaturnBundleComponentInfo(String bundlesSymbolicName) {
		this.bundlesSymbolicName = bundlesSymbolicName;
		this.componentInfoMap = new HashMap<String, IComponentInfo>();
	}

	public void addComponentInfo(IComponentInfo componentInfo)
			throws SaturnComponentException {

		validateComponentInfoKey(componentInfo);
		this.componentInfoMap.put(componentInfo.getKey(), componentInfo);
		logger.debug("add componentInfo \"" + componentInfo.getKey()
				+ "\" from " + this.bundlesSymbolicName + " bundle");
	}

	public void addComponentInfos(Collection<IComponentInfo> componentInfos)
			throws SaturnComponentException {

		if (componentInfos != null) {

			for (IComponentInfo componentInfo : componentInfos) {
				validateComponentInfoKey(componentInfo);
			}

			for (IComponentInfo componentInfo : componentInfos) {
				this.addComponentInfo(componentInfo);
			}
		}
	}

	public String getBundleSymbolicName() {
		return this.bundlesSymbolicName;
	}

	public Collection<IComponentInfo> getComponentInfos() {
		return this.componentInfoMap.values();
	}

	public Set<String> getComponentKeys() {
		return this.componentInfoMap.keySet();
	}

	public IComponentInfo getComponentInfo(String componentKey) {
		return this.componentInfoMap.get(componentKey);
	}

	private void validateComponentInfoKey(IComponentInfo componentInfo)
			throws SaturnComponentException {

		if (componentInfo.getKey() == null) {
			logger
					.error("add componentInfo failed, because the componentInfo key is null");

			throw new SaturnComponentException(
					"add componentInfo failed, because the componentInfo key is null");
		}

		if (this.componentInfoMap.containsKey(componentInfo.getKey())) {
			logger
					.warn("add componentInfo failed, because the componentInfo key ["
							+ componentInfo.getKey() + "] is already existed");

//			throw new SaturnComponentException(
//					"add componentInfo failed, because the componentInfo key ["
//							+ componentInfo.getKey() + "] is already existed");
		}
	}
}
